package com.lzh.persevere.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lzh.persevere.common.R;
import com.lzh.persevere.common.ResultUtil;
import com.lzh.persevere.model.entity.Photo;
import com.lzh.persevere.model.entity.PhotoTag;
import com.lzh.persevere.model.ro.photoTag.AddPhotoTagRO;
import com.lzh.persevere.model.vo.photoTag.PhotoTagVO;
import com.lzh.persevere.service.PhotoService;
import com.lzh.persevere.service.PhotoTagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 照片标签controller
 *
 * @author luzhiheng
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/photoTag")
public class PhotoTagController {

    @Resource
    private PhotoTagService photoTagService;

    @Resource
    private PhotoService photoService;

    @GetMapping("/findList")
    public R<List<PhotoTagVO>> findList() {
        List<PhotoTag> photoTagList = photoTagService.list();
        List<PhotoTagVO> photoTagVOList = photoTagList.stream()
                .map(e -> {
                    PhotoTagVO vo = new PhotoTagVO();
                    vo.setId(e.getId());
                    vo.setTagName(e.getTagName());
                    vo.setCount(photoService.count(Wrappers.<Photo>lambdaQuery().eq(Photo::getTagId, e.getId())));
                    return vo;
                }).collect(Collectors.toList());
        return ResultUtil.success(photoTagVOList);
    }

    @PostMapping("/addTag")
    public R<Boolean> addTag(@RequestBody @Valid AddPhotoTagRO ro) {
        boolean flag = photoTagService.save(new PhotoTag().setTagName(ro.getTagName()));
        return ResultUtil.success(flag);
    }
}
